import csv
import math
import os


def read_csv(csv_name):
    f = open(csv_name, 'r')
    reader = csv.reader(f)
    result = []
    for row in reader:
        row = [float(s) for s in row]
        result.append(row)
    return result

def write_result(result):
    f = open('data_aggregation_result.csv', 'w', encoding='utf-8', newline='')
    csv_writer = csv.writer(f)
    for res in result:
        csv_writer.writerow(res)
    f.close()

def keypoint_value(input_example):
    if not os.path.exists(input_example):
        return '不存在此文件！！！'
    keypoints = read_csv(input_example)
    length=len(keypoints)
    target = [0 for i in range(8)]
    for keypoint in keypoints:
        for i in range(1,9):
            target[i-1]+=keypoint[i]
    target=[i/length for i in target]
    value=0
    for tar in target:
        value+=tar**2
    value=value**0.5

    values=[]
    for i in range(length):
        target = [0 for i in range(8)]
        for j in range(length):
            if i==j:
                continue
            else:
                keypoint=keypoints[j]
                for k in range(1,9):
                    target[k-1]+=keypoint[k]
        target=[i/(length-1) for i in target]
        value_one = 0
        for tar in target:
            value_one += tar ** 2
        value_one = value_one ** 0.5
        values.append(value_one)

    affects=[]
    for value_p in values:
        affect= math.fabs(value_p-value)
        affects.append(affect)
    return affects.index(min(affects))

input_example = 'data_aggregation_2.csv'
output_example = '不存在此文件！！！'
output =keypoint_value(input_example)


def compare(o_e, o):
    #修改比较方式
    return o==o_e

print("")

# 用例目的
print("该用例目的为：测试是否正常产生压缩点")

print("")

# 子用例编号
print("子用例编号：keypoint_value_2")

print("")

print("****************************")
print("当前输入为：")
print(input_example)

print("")

print("****************************")
print("当前输出为:")
# 输出处理后数据
print(output)

print("")

print("****************************")
print("预设输出为:")
# 输出处理后数据
print(output_example)

print("")

print("****************************")
print("是否正确:")
# 输出对比结果
# 需要写一个compare函数
if compare(output_example, output):
    print("输出与预定目标相符")
else:
    print("输出与预定目标不符")
# 输出对比结果

print("\n")
